###############################################################################################################################################
### Setup

# Set working directory
setwd('~/Dropbox/Projects/Minority Party/Paper 2/APSR Replication Files/')

# Source setup file
source('setup.R')


### Format parent data

# Load bills data
bills <- fread(file='bills97-109_partyPriorities_issuePreferences.csv', data.table=FALSE, stringsAsFactors = FALSE)

# Indicators for the two periods
bills$period <- 1
bills$period[bills$congress %in% 104:109] <- 2

# Unique topics
topics <- c(1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)

# Names of topics
topicNames <- c('Macroeconomics', 'Civil Rights', 'Health', 'Agriculture', 'Labor', 'Education',
                'Environment', 'Energy', 'Transportation', 'Law and Crime', 'Social Welfare',
                'Housing', 'Domestic Commerce', 'Defense', 'Technology', 'Foreign Trade',
                'International Affairs', 'Government Operations', 'Public Lands')

# Data frame with topic codes and names, for joining later
topicDF <- data.frame(major=topics, names=topicNames)

# Define plot colors
cols <- brewer.pal(6, 'Greys') %>% .[c(4,6)]

# Remove nonsubstantive bills
bills %<>% filter(substantive==1)


### Figure 1 data

# Data frame with proportion of bills in each issue receiving a vote
issues <- bills %>%
  group_by(period, major) %>%
  dplyr::summarise(
    n=n(),
    voted=table(pv_all)['1'],
    prop=voted/n,
    min=table(majority)['0'],
    minVoted=table(pv_all[majority==0])['1'],
    votedMin=minVoted/min,
    minProp=minVoted/n,
    maj=table(majority)['1'],
    majVoted=table(pv_all[majority==1])['1'],
    votedMaj=majVoted/maj,
    majProp=majVoted/n
  ) %>%
  ungroup()
issues <- left_join(issues, topicDF, by='major')

# Filter out NAs, 99s
issues <- filter(issues, !is.na(major) & major %in% topics)

# Split by period, order low to high
issues1 <- filter(issues, period==1) %>% arrange(prop)
issues2 <- filter(issues, period==2) %>% arrange(prop)


### Figure 2 data

# Data frame with minSD and majSD by period and major topic
spread <- bills %>%
  filter(!is.na(major)) %>%
  group_by(period, major) %>%
  dplyr::summarise(
    minSpread=unique(minSD[majority==0]),
    majSpread=unique(majSD[majority==1]), 
    numVote=table(pv_all) %>% .['1']
  ) %>%
  ungroup()

# Join spread and topicDF
spread <- left_join(spread, topicDF, by='major')

# Filter out any NAs
spread %<>% filter(!is.na(minSpread) & !is.na(majSpread))

# Split DFs by period
spread1 <- filter(spread, period==1)
spread2 <- filter(spread, period==2)



###############################################################################################################################################
### Figure 1

# Plot the *number* of bills receiving consideration in each period across issues
pdf('figure1.pdf', width=10, height=12)
par(mar=c(10.1, 4.1, 4.1, 2.1), mfrow=c(2,1))
issueVote1 <- barplot(arrange(issues1, voted) %>% pull(voted), ylim=c(0,750), 
                      main='Democatic Majority (99th-103rd)', ylab='Number Reaching Vote', xaxt='n')
text(x=issueVote1, y=arrange(issues1, voted) %>% pull(voted), labels=arrange(issues1, voted) %>% pull(voted) %>% round(., 2), pos=3)
axis(at=issueVote1, side=1, labels=arrange(issues1, voted) %>% pull(names), las=2)

issueVote2 <- barplot(arrange(issues2, voted) %>% pull(voted), ylim=c(0,750), 
                      main='Republican Majority (104th-109th)', ylab='Number Reaching Vote', xaxt='n')
text(x=issueVote2, y=arrange(issues2, voted) %>% pull(voted), labels=arrange(issues2, voted) %>% pull(voted) %>% round(., 2), pos=3)
axis(at=issueVote2, side=1, labels=arrange(issues2, voted) %>% pull(names), las=2)
par(mar=c(5.1, 4.1, 4.1, 2.1), mfrow=c(1,1))
dev.off()



###############################################################################################################################################
### Figure 2

# Plot majority and minority spread by issue and period
pdf('figure2.pdf', width=20, height=10)
par(mgp=c(1,1,0), par(mfrow=c(1,2)))
plot(spread1$majSpread, spread1$minSpread, xlab='Majority Spread', ylab='Minority Spread', cex=1.5,
     pch=16, xaxt='n', yaxt='n', xlim=c(.21, .47), ylim=c(.13, .39), main='Democratic Majority (99th-103rd)',
     cex.lab=1.5, cex.main=1.5, type='n')
with(spread1, symbols(x=majSpread, y=minSpread, circles=numVote, inches=1/5, bg='gray30', fg=NULL, add=TRUE))
abline(v=median(spread1$majSpread), lty=2)
abline(h=median(spread1$minSpread), lty=2)
text(x=spread1$majSpread, y=spread1$minSpread, labels=spread1$names, pos=3, offset = 0.5)
legend('topleft', 'Majority Unity\nMinority Disunity', bty='n', cex=1.5)
legend('topright', 'Majority Disunity\nMinority Disunity', bty='n', cex=1.5)
legend('bottomleft', 'Majority Unity\nMinority Unity', bty='n', cex=1.5)
legend('bottomright', 'Majority Disunity\nMinority Unity', bty='n', cex=1.5)

plot(spread2$majSpread, spread2$minSpread, xlab='Majority Spread', ylab='Minority Spread', cex=1.5,
     pch=16, xaxt='n', yaxt='n', xlim=c(.11, .35), ylim=c(.13, .39), main='Republican Majority (104th-109th)',
     cex.lab=1.5, cex.main=1.5, type='n')
with(spread2, symbols(x=majSpread, y=minSpread, circles=numVote, inches=1/5, bg='gray30', fg=NULL, add=TRUE))
abline(v=median(spread2$majSpread), lty=2)
abline(h=median(spread2$minSpread), lty=2)
text(x=spread2$majSpread, y=spread2$minSpread, labels=spread2$names, pos=3, offset = 0.5)
legend('topleft', 'Majority Unity\nMinority Disunity', bty='n', cex=1.5)
legend('topright', 'Majority Disunity\nMinority Disunity', bty='n', cex=1.5)
legend('bottomleft', 'Majority Unity\nMinority Unity', bty='n', cex=1.5)
legend('bottomright', 'Majority Disunity\nMinority Unity', bty='n', cex=1.5)
par(mgp=c(3,1,0), mfrow=c(1,1))
dev.off()